<%--
  Created by IntelliJ IDEA.
  User: 11954
  Date: 2022/4/16
  Time: 10:21
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!-- 引入jstl的函数 -->
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ taglib prefix="C" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>查询班级别表</title>
    <script>
        //全选
        var checkeallname  = document.getElementById("checkall");
        var checknames = document.getElementsByName("checknames");
        function checkallbox(flag) {

            for (var i = 0;i < checknames.length;i++) {
                checknames[i].checked = flag;
            }
        }

    window.onload=function () {
        //反选
        var checkeallname  = document.getElementById("checkall");
        var checknames = document.getElementsByName("checknames");

        for(var i=0;i<checknames.length;i++){
            checknames[i].addEventListener("click",function(){
                var temp =true;
                for(var j=0;j<checknames.length;j++){
                    if (!checknames[j].checked) {
                        temp =false;
                        break;
                    }
                }
                checkeallname.checked = temp;
            });
        }
    }

        //判断复选框勾选的数量
        var checknames = document.getElementsByName("checknames");
        function getchecked() {
            var count = 0;
            for (var i = 0; i < checknames.length; i++) {
                if (checknames[i].checked){
                    count++;
                }
            }
            return count;
        }
        //js代码修改表单action实现删除按钮提交表单；如果用户没有勾选，则提交不了，也就是没有后续的删除操作

        function delsubform() {
            var myform = document.getElementById("myform");
            var count = getchecked();
            if(count<1){
                alert("删除班级至少选择一个班级！");
                return;
            }
                myform.action="classesdel";
                myform.submit();
        }
        //修改班级判断选中班级是否为1，并提交表单，回显要修改的学生的信息
        function updatesubform() {
            var count = getchecked();
            if(count !=1 ){
                alert("修改班级只能选择一个班级！");
                return;
            }
            myform.action="showupdateclassServlset";
            myform.submit();
        }
    </script>
</head>
<body>
    <button onclick="javascript:location.href='insertclass.jsp'">新增班级</button>
    <button onclick="delsubform()">删除班级</button>
    <button onclick="updatesubform()">修改班级</button>
    <form id="myform"  action="classesdel" method="post">
        <c:if test="${not empty page.datas}">
            <table border="1">
                <tr>
                    <th>全选<input id="checkall" type="checkbox" onclick="checkallbox(this.checked)"></th>
                    <th>id</th>
                    <th>班级名</th>
                    <th>班级人数</th>
                </tr>
                <c:forEach items="${page.datas}" var="classes">
                    <tr>
                        <td><input name="checknames" type="checkbox" value="${classes.id}"></td>
                        <td>&nbsp;&nbsp;&nbsp;${classes.id}&nbsp;&nbsp;&nbsp;</td>
                        <td>${classes.class_name}</td>
                        <td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;${classes.class_count}</td>
                    </tr>
                </c:forEach>
            </table>
        </c:if>
        <c:if test="${ empty page.datas}">
            <h2>班级信息为空</h2>
        </c:if>
    </form>
    <C:if test="${page.page==1}">
        <a href='classesquerypage?page=1&pagesize=3'>首页</a>
        <a href='classesquerypage?page=${page.page+1}&pagesize=3'>下一页</a>
        <a href='classesquerypage?page=${page.pageCount}&pagesize=3'>尾页</a>
    </C:if>
<C:if test="${page.page > 1 && page.page<page.pageCount}">
    <a href='classesquerypage?page=1&pagesize=3'>首页</a>
    <a href='classesquerypage?page=${page.page-1}&pagesize=3'>上一页</a>
    <a href='classesquerypage?page=${page.page+1}&pagesize=3'>下一页</a>
    <a href='classesquerypage?page=${page.pageCount}&pagesize=3'>尾页</a>
</C:if>
<C:if test="${page.page==page.pageCount}">
    <a href='classesquerypage?page=1&pagesize=3'>首页</a>
    <a href='classesquerypage?page=${page.page-1}&pagesize=3'>上一页</a>
</C:if>
    <a>第${page.page}页</a>/<a>共${page.pageCount}页</a>
    <a href="index.jsp">返回主页</a>
</body>
</html>
